package cn.blunce.weather.models;

import java.util.List;
import java.util.ArrayList;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;

import de.greenrobot.dao.AbstractDao;
import de.greenrobot.dao.Property;
import de.greenrobot.dao.internal.SqlUtils;
import de.greenrobot.dao.internal.DaoConfig;
import de.greenrobot.dao.query.Query;
import de.greenrobot.dao.query.QueryBuilder;

import cn.blunce.weather.models.DailyForcast;

// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
/** 
 * DAO for table "DAILY_FORCAST".
*/
public class DailyForcastDao extends AbstractDao<DailyForcast, Long> {

    public static final String TABLENAME = "DAILY_FORCAST";

    /**
     * Properties of entity DailyForcast.<br/>
     * Can be used for QueryBuilder and for referencing column names.
    */
    public static class Properties {
        public final static Property Id = new Property(0, Long.class, "id", true, "_id");
        public final static Property Astro_ID = new Property(1, Long.class, "astro_ID", false, "ASTRO__ID");
        public final static Property Cond_ID = new Property(2, Long.class, "cond_ID", false, "COND__ID");
        public final static Property Date = new Property(3, String.class, "date", false, "DATE");
        public final static Property Hum = new Property(4, Double.class, "hum", false, "HUM");
        public final static Property Pcpn = new Property(5, Double.class, "pcpn", false, "PCPN");
        public final static Property Pop = new Property(6, Double.class, "pop", false, "POP");
        public final static Property Pres = new Property(7, Double.class, "pres", false, "PRES");
        public final static Property Vis = new Property(8, Double.class, "vis", false, "VIS");
        public final static Property Tmp_ID = new Property(9, Long.class, "tmp_ID", false, "TMP__ID");
        public final static Property Wind_ID = new Property(10, Long.class, "wind_ID", false, "WIND__ID");
        public final static Property Weather_message_id = new Property(11, Long.class, "weather_message_id", false, "WEATHER_MESSAGE_ID");
    };

    private DaoSession daoSession;

    private Query<DailyForcast> weatherMessage_Daily_forcastsQuery;

    public DailyForcastDao(DaoConfig config) {
        super(config);
    }
    
    public DailyForcastDao(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
        this.daoSession = daoSession;
    }

    /** Creates the underlying database table. */
    public static void createTable(SQLiteDatabase db, boolean ifNotExists) {
        String constraint = ifNotExists? "IF NOT EXISTS ": "";
        db.execSQL("CREATE TABLE " + constraint + "\"DAILY_FORCAST\" (" + //
                "\"_id\" INTEGER PRIMARY KEY ," + // 0: id
                "\"ASTRO__ID\" INTEGER," + // 1: astro_ID
                "\"COND__ID\" INTEGER," + // 2: cond_ID
                "\"DATE\" TEXT," + // 3: date
                "\"HUM\" REAL," + // 4: hum
                "\"PCPN\" REAL," + // 5: pcpn
                "\"POP\" REAL," + // 6: pop
                "\"PRES\" REAL," + // 7: pres
                "\"VIS\" REAL," + // 8: vis
                "\"TMP__ID\" INTEGER," + // 9: tmp_ID
                "\"WIND__ID\" INTEGER," + // 10: wind_ID
                "\"WEATHER_MESSAGE_ID\" INTEGER);"); // 11: weather_message_id
    }

    /** Drops the underlying database table. */
    public static void dropTable(SQLiteDatabase db, boolean ifExists) {
        String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"DAILY_FORCAST\"";
        db.execSQL(sql);
    }

    /** @inheritdoc */
    @Override
    protected void bindValues(SQLiteStatement stmt, DailyForcast entity) {
        stmt.clearBindings();
 
        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
 
        Long astro_ID = entity.getAstro_ID();
        if (astro_ID != null) {
            stmt.bindLong(2, astro_ID);
        }
 
        Long cond_ID = entity.getCond_ID();
        if (cond_ID != null) {
            stmt.bindLong(3, cond_ID);
        }
 
        String date = entity.getDate();
        if (date != null) {
            stmt.bindString(4, date);
        }
 
        Double hum = entity.getHum();
        if (hum != null) {
            stmt.bindDouble(5, hum);
        }
 
        Double pcpn = entity.getPcpn();
        if (pcpn != null) {
            stmt.bindDouble(6, pcpn);
        }
 
        Double pop = entity.getPop();
        if (pop != null) {
            stmt.bindDouble(7, pop);
        }
 
        Double pres = entity.getPres();
        if (pres != null) {
            stmt.bindDouble(8, pres);
        }
 
        Double vis = entity.getVis();
        if (vis != null) {
            stmt.bindDouble(9, vis);
        }
 
        Long tmp_ID = entity.getTmp_ID();
        if (tmp_ID != null) {
            stmt.bindLong(10, tmp_ID);
        }
 
        Long wind_ID = entity.getWind_ID();
        if (wind_ID != null) {
            stmt.bindLong(11, wind_ID);
        }
 
        Long weather_message_id = entity.getWeather_message_id();
        if (weather_message_id != null) {
            stmt.bindLong(12, weather_message_id);
        }
    }

    @Override
    protected void attachEntity(DailyForcast entity) {
        super.attachEntity(entity);
        entity.__setDaoSession(daoSession);
    }

    /** @inheritdoc */
    @Override
    public Long readKey(Cursor cursor, int offset) {
        return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0);
    }    

    /** @inheritdoc */
    @Override
    public DailyForcast readEntity(Cursor cursor, int offset) {
        DailyForcast entity = new DailyForcast( //
            cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id
            cursor.isNull(offset + 1) ? null : cursor.getLong(offset + 1), // astro_ID
            cursor.isNull(offset + 2) ? null : cursor.getLong(offset + 2), // cond_ID
            cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3), // date
            cursor.isNull(offset + 4) ? null : cursor.getDouble(offset + 4), // hum
            cursor.isNull(offset + 5) ? null : cursor.getDouble(offset + 5), // pcpn
            cursor.isNull(offset + 6) ? null : cursor.getDouble(offset + 6), // pop
            cursor.isNull(offset + 7) ? null : cursor.getDouble(offset + 7), // pres
            cursor.isNull(offset + 8) ? null : cursor.getDouble(offset + 8), // vis
            cursor.isNull(offset + 9) ? null : cursor.getLong(offset + 9), // tmp_ID
            cursor.isNull(offset + 10) ? null : cursor.getLong(offset + 10), // wind_ID
            cursor.isNull(offset + 11) ? null : cursor.getLong(offset + 11) // weather_message_id
        );
        return entity;
    }
     
    /** @inheritdoc */
    @Override
    public void readEntity(Cursor cursor, DailyForcast entity, int offset) {
        entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
        entity.setAstro_ID(cursor.isNull(offset + 1) ? null : cursor.getLong(offset + 1));
        entity.setCond_ID(cursor.isNull(offset + 2) ? null : cursor.getLong(offset + 2));
        entity.setDate(cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3));
        entity.setHum(cursor.isNull(offset + 4) ? null : cursor.getDouble(offset + 4));
        entity.setPcpn(cursor.isNull(offset + 5) ? null : cursor.getDouble(offset + 5));
        entity.setPop(cursor.isNull(offset + 6) ? null : cursor.getDouble(offset + 6));
        entity.setPres(cursor.isNull(offset + 7) ? null : cursor.getDouble(offset + 7));
        entity.setVis(cursor.isNull(offset + 8) ? null : cursor.getDouble(offset + 8));
        entity.setTmp_ID(cursor.isNull(offset + 9) ? null : cursor.getLong(offset + 9));
        entity.setWind_ID(cursor.isNull(offset + 10) ? null : cursor.getLong(offset + 10));
        entity.setWeather_message_id(cursor.isNull(offset + 11) ? null : cursor.getLong(offset + 11));
     }
    
    /** @inheritdoc */
    @Override
    protected Long updateKeyAfterInsert(DailyForcast entity, long rowId) {
        entity.setId(rowId);
        return rowId;
    }
    
    /** @inheritdoc */
    @Override
    public Long getKey(DailyForcast entity) {
        if(entity != null) {
            return entity.getId();
        } else {
            return null;
        }
    }

    /** @inheritdoc */
    @Override    
    protected boolean isEntityUpdateable() {
        return true;
    }
    
    /** Internal query to resolve the "daily_forcasts" to-many relationship of WeatherMessage. */
    public List<DailyForcast> _queryWeatherMessage_Daily_forcasts(Long weather_message_id) {
        synchronized (this) {
            if (weatherMessage_Daily_forcastsQuery == null) {
                QueryBuilder<DailyForcast> queryBuilder = queryBuilder();
                queryBuilder.where(Properties.Weather_message_id.eq(null));
                weatherMessage_Daily_forcastsQuery = queryBuilder.build();
            }
        }
        Query<DailyForcast> query = weatherMessage_Daily_forcastsQuery.forCurrentThread();
        query.setParameter(0, weather_message_id);
        return query.list();
    }

    private String selectDeep;

    protected String getSelectDeep() {
        if (selectDeep == null) {
            StringBuilder builder = new StringBuilder("SELECT ");
            SqlUtils.appendColumns(builder, "T", getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T0", daoSession.getAstroWeatherDao().getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T1", daoSession.getWeatherDailyDao().getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T2", daoSession.getWeatherTmpDao().getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T3", daoSession.getWeatherWindDao().getAllColumns());
            builder.append(" FROM DAILY_FORCAST T");
            builder.append(" LEFT JOIN ASTRO_WEATHER T0 ON T.\"ASTRO__ID\"=T0.\"_id\"");
            builder.append(" LEFT JOIN WEATHER_DAILY T1 ON T.\"COND__ID\"=T1.\"_id\"");
            builder.append(" LEFT JOIN WEATHER_TMP T2 ON T.\"TMP__ID\"=T2.\"_id\"");
            builder.append(" LEFT JOIN WEATHER_WIND T3 ON T.\"WIND__ID\"=T3.\"_id\"");
            builder.append(' ');
            selectDeep = builder.toString();
        }
        return selectDeep;
    }
    
    protected DailyForcast loadCurrentDeep(Cursor cursor, boolean lock) {
        DailyForcast entity = loadCurrent(cursor, 0, lock);
        int offset = getAllColumns().length;

        AstroWeather astro = loadCurrentOther(daoSession.getAstroWeatherDao(), cursor, offset);
        entity.setAstro(astro);
        offset += daoSession.getAstroWeatherDao().getAllColumns().length;

        WeatherDaily cond = loadCurrentOther(daoSession.getWeatherDailyDao(), cursor, offset);
        entity.setCond(cond);
        offset += daoSession.getWeatherDailyDao().getAllColumns().length;

        WeatherTmp tmp = loadCurrentOther(daoSession.getWeatherTmpDao(), cursor, offset);
        entity.setTmp(tmp);
        offset += daoSession.getWeatherTmpDao().getAllColumns().length;

        WeatherWind wind = loadCurrentOther(daoSession.getWeatherWindDao(), cursor, offset);
        entity.setWind(wind);

        return entity;    
    }

    public DailyForcast loadDeep(Long key) {
        assertSinglePk();
        if (key == null) {
            return null;
        }

        StringBuilder builder = new StringBuilder(getSelectDeep());
        builder.append("WHERE ");
        SqlUtils.appendColumnsEqValue(builder, "T", getPkColumns());
        String sql = builder.toString();
        
        String[] keyArray = new String[] { key.toString() };
        Cursor cursor = db.rawQuery(sql, keyArray);
        
        try {
            boolean available = cursor.moveToFirst();
            if (!available) {
                return null;
            } else if (!cursor.isLast()) {
                throw new IllegalStateException("Expected unique result, but count was " + cursor.getCount());
            }
            return loadCurrentDeep(cursor, true);
        } finally {
            cursor.close();
        }
    }
    
    /** Reads all available rows from the given cursor and returns a list of new ImageTO objects. */
    public List<DailyForcast> loadAllDeepFromCursor(Cursor cursor) {
        int count = cursor.getCount();
        List<DailyForcast> list = new ArrayList<DailyForcast>(count);
        
        if (cursor.moveToFirst()) {
            if (identityScope != null) {
                identityScope.lock();
                identityScope.reserveRoom(count);
            }
            try {
                do {
                    list.add(loadCurrentDeep(cursor, false));
                } while (cursor.moveToNext());
            } finally {
                if (identityScope != null) {
                    identityScope.unlock();
                }
            }
        }
        return list;
    }
    
    protected List<DailyForcast> loadDeepAllAndCloseCursor(Cursor cursor) {
        try {
            return loadAllDeepFromCursor(cursor);
        } finally {
            cursor.close();
        }
    }
    

    /** A raw-style query where you can pass any WHERE clause and arguments. */
    public List<DailyForcast> queryDeep(String where, String... selectionArg) {
        Cursor cursor = db.rawQuery(getSelectDeep() + where, selectionArg);
        return loadDeepAllAndCloseCursor(cursor);
    }
 
}
